﻿<template>
  <header class="sticky top-0 z-100">
    <nav class="bg-white border-gray-200 border-b dark:bg-gray-900">
      <div class="max-w-screen-xl flex flex-wrap items-center justify-between mx-auto p-4">
        <!-- LOGO -->
        <a href="/" class="flex items-center">
          <img src="https://flowbite.com/docs/images/logo.svg" class="h-8 mr-3" alt="Flowbite Logo" />
          <span class="self-center text-2xl font-semibold whitespace-nowrap dark:text-white">
            {{ $store.state.setting.blogName }}
          </span>
        </a>

        <div class="flex items-center md:order-2">
          <div class="flex mr-3">
            <button type="button" data-collapse-toggle="navbar-search" aria-controls="navbar-search"
                    aria-expanded="false"
                    class="md:hidden text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:outline-none focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-600 rounded-lg text-sm p-2.5 mr-1">
              <svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                   viewBox="0 0 20 20">
                <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                      d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z" />
              </svg>
              <span class="sr-only">鎼滅储</span>
            </button>
            <div class="relative hidden md:block">
              <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
                <svg class="w-4 h-4 text-gray-500 dark:text-gray-400" aria-hidden="true"
                     xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
                  <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
                        stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z" />
                </svg>
                <span class="sr-only">Search icon</span>
              </div>
              <input type="text" id="search-navbar"
                     class="block w-full p-2 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                     placeholder="鎼滅储寰呭紑鍙?..">
            </div>
            <button data-collapse-toggle="navbar-search" type="button"
                    class="inline-flex items-center p-2 w-10 h-10 justify-center text-sm text-gray-500 rounded-lg md:hidden hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:text-gray-400 dark:hover:bg-gray-700 dark:focus:ring-gray-600"
                    aria-controls="navbar-search" aria-expanded="false">
              <span class="sr-only">Open main menu</span>
              <svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                   viewBox="0 0 17 14">
                <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                      d="M1 1h15M1 7h15M1 13h15" />
              </svg>
            </button>
          </div>

          <!-- 鐧诲綍鎸夐挳鎴栫敤鎴峰ご鍍忓睍绀?-->
          <span class="text-gray-900 hover:text-blue-700" v-if="!isLogin" @click="$router.push('/login')">鐧诲綍</span>
          <button type="button" v-else
                  class="flex mr-3 text-sm rounded-full md:mr-0 focus:ring-4 focus:ring-gray-300 dark:focus:ring-gray-600"
                  id="user-menu-button" aria-expanded="false" data-dropdown-toggle="user-dropdown"
                  data-dropdown-placement="bottom">
            <span class="sr-only">Open user menu</span>
            <!-- 鐢ㄦ埛鐧诲綍澶村儚 -->
            <img class="w-8 h-8 rounded-full" :src="$store.state.setting.avatar" alt="user photo">
          </button>
          <!-- Dropdown menu -->
          <div class="z-50 hidden my-4 text-base list-none bg-white divide-y divide-gray-100 rounded-lg shadow dark:bg-gray-700 dark:divide-gray-600"
               id="user-dropdown">
            <ul class="py-2" aria-labelledby="user-menu-button">
              <li>
                <a @click="enterBackend"
                   class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white">
                  <svg class="inline w-3 h-3 mb-2px mr-1 text-gray-700 dark:text-white" aria-hidden="true"
                       xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
                    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
                          stroke-width="2"
                          d="M10 14v4m-4 1h8M1 10h18M2 1h16a1 1 0 0 1 1 1v11a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1Z" />
                  </svg>
                  杩涘叆鍚庡彴
                </a>
              </li>
              <li>
                <a @click="logout"
                   class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white">
                  <svg class="inline w-3 h-3 mb-2px mr-1 text-gray-700 dark:text-white" aria-hidden="true"
                       xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16">
                    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
                          stroke-width="2"
                          d="M4 8h11m0 0-4-4m4 4-4 4m-5 3H3a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2h3" />
                  </svg>
                  閫€鍑虹櫥褰?
                </a>
              </li>
            </ul>
          </div>
        </div>
        <div class="items-center justify-between hidden w-full md:flex md:w-auto md:order-1" id="navbar-search">
          <ul
              class="flex flex-col p-4 md:p-0 mt-4 font-medium border border-gray-100 rounded-lg bg-gray-50 md:flex-row md:space-x-8 md:mt-0 md:border-0 md:bg-white dark:bg-gray-800 md:dark:bg-gray-900 dark:border-gray-700">
            <li>
              <a @click="$router.push('/')" :class="[currPath == '/' ? 'text-blue-700' : 'text-gray-900']"
                 class="block py-2 pl-3 pr-4 rounded md:bg-transparent md:p-0 md:dark:text-blue-500" aria-current="page">
                棣栭〉
              </a>
            </li>
            <li>
              <a @click="$router.push('/category')"
                 :class="[currPath == '/category' ? 'text-blue-700' : 'text-gray-900']"
                 class="block py-2 pl-3 pr-4 rounded hover:bg-gray-100 md:hover:bg-transparent md:hover:text-blue-700 md:p-0 md:dark:hover:text-blue-500 dark:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700">
                鍒嗙被
              </a>
            </li>
            <li>
              <a @click="$router.push('/tag')"
                 :class="[currPath == '/tag' ? 'text-blue-700' : 'text-gray-900']"
                 class="block py-2 pl-3 pr-4 rounded hover:bg-gray-100 md:hover:bg-transparent md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700">
                鏍囩
              </a>
            </li>
            <li>
              <a @click="$router.push('/archive')"
                 :class="[currPath == '/archive' ? 'text-blue-700' : 'text-gray-900']"
                 class="block py-2 pl-3 pr-4 rounded hover:bg-gray-100 md:hover:bg-transparent md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700">
                褰掓。
              </a>
            </li>
            <li>
              <a @click="$router.push('/notices')"
                 :class="[currPath == '/notices' ? 'text-blue-700' : 'text-gray-900']"
                 class="block py-2 pl-3 pr-4 rounded hover:bg-gray-100 md:hover:bg-transparent md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700">
                鍏憡
              </a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <!-- 鍏憡鏍?-->
    <div class="max-w-screen-xl mx-auto">
      <NoticesList />
    </div>

    <!-- 閫€鍑虹櫥褰?modal -->
    <div id="logout-modal" tabindex="-1"
         class="fixed top-0 left-0 right-0 z-50 hidden p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
      <div class="relative w-full max-w-md max-h-full">
        <div class="relative bg-white rounded-lg shadow dark:bg-gray-700">
          <button type="button"
                  class="absolute top-3 right-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ml-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white"
                  data-modal-hide="logout-modal">
            <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                 viewBox="0 0 14 14">
              <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                    d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" />
            </svg>
            <span class="sr-only">Close modal</span>
          </button>
          <div class="p-6 text-center">
            <svg class="mx-auto mb-4 text-gray-400 w-12 h-12 dark:text-gray-200" aria-hidden="true"
                 xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
              <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                    d="M10 11V6m0 8h.01M19 10a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
            </svg>
            <h3 class="mb-5 text-lg font-normal text-gray-500 dark:text-gray-400">鏄惁閫€鍑虹櫥褰?</h3>
            <button @click="logout" data-modal-hide="logout-modal" type="button"
                    class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800 font-medium rounded-lg text-sm inline-flex items-center px-5 py-2.5 text-center mr-2">
              纭
            </button>
            <button data-modal-hide="logout-modal" type="button"
                    class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-gray-200 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-600">
              鍙栨秷
            </button>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { showModel, showMessage } from '@/composables/util'
import { initDropdowns, initCollapses, initModals, Modal } from 'flowbite'
import NoticesList from '@/components/notices/NoticesList.vue'

onMounted(() => {
  initCollapses()
  initDropdowns()
  initModals()
  
  // 鐩戝惉鐐瑰嚮鑳屾櫙鍏抽棴妯℃€佹鐨勪簨浠?
  document.addEventListener('click', (e) => {
    // 濡傛灉鎼滅储妯℃€佹姝ｅ湪鏄剧ず锛屽苟涓旂偣鍑讳簡鑳屾櫙鍖哄煙
    if (showingSearchModal.value) {
      const searchModal = document.getElementById('search-modal')
      const modalContent = searchModal?.querySelector('.bg-white')
      
      // 濡傛灉鐐瑰嚮鐨勬槸鑳屾櫙鑰屼笉鏄ā鎬佹鍐呭
      if (searchModal && !modalContent?.contains(e.target) && searchModal.contains(e.target)) {
        closeSearchModal()
      }
    }
  })
})

onBeforeUnmount(() => {
  // 缁勪欢鍗歌浇鍓嶇‘淇濇竻鐞嗘墍鏈夋ā鎬佹娈嬬暀鏁堟灉
  if (showingSearchModal.value) {
    cleanupModalEffects()
  }
})

const store = useStore()
const route = useRoute()
const router = useRouter()

const currPath = ref(route.path)
const isLogin = computed(() => Object.keys(store.state.user).length > 0)
// 浣跨敤 Vuex 涓殑鐢ㄦ埛淇℃伅鑾峰彇瑙掕壊锛屽亣璁?user.role 涓?1 琛ㄧず绠＄悊鍛橈紝鍏跺畠涓烘櫘閫氱敤鎴?
const userRole = computed(() => store.state.user?.role)

const logout = () => {
  console.log('鐧诲嚭')
  store.dispatch('logout')
  showMessage('閫€鍑虹櫥褰曟垚鍔?, 'success')
}

const enterBackend = () => {
  // 浠?localStorage 涓幏鍙栬鑹蹭俊鎭?
  const role = localStorage.getItem('role')
  if (role === '1' || role === 1) {
    router.push('/admin')
  } else {
    router.push('/user')
  }
}

// 绉婚櫎妯℃€佹鑳屾櫙鍜屾仮澶嶆粴鍔ㄧ殑helper鍑芥暟
const cleanupModalEffects = () => {
  // 鎵嬪姩绉婚櫎鑳屾櫙閬僵 - 灏濊瘯澶氱鍙兘鐨勭被鍚嶅拰鍏冪礌
  document.querySelectorAll('.modal-backdrop, div[class*="bg-gray-900"], div[class*="opacity"]').forEach(el => {
    el.remove()
  })
  
  // 鏌ユ壘鍙兘琚坊鍔犵殑鍏朵粬閬僵灞?
  document.querySelectorAll('div[style*="position: fixed"]').forEach(el => {
    if (el.style.zIndex > 30 && !el.id.includes('modal')) {
      el.remove()
    }
  })
  
  // 鎭㈠body婊氬姩鍜屾牱寮?
  document.body.classList.remove('overflow-hidden')
  document.body.classList.remove('overflow-y-hidden')
  document.body.style.paddingRight = ''
  document.body.style.overflow = ''
  
  // 璁剧疆body鍜宧tml涓?00%閫忔槑鑳屾櫙
  document.body.style.backgroundColor = 'transparent'
  document.documentElement.style.backgroundColor = 'transparent'
}

const closeSearchModal = () => {
  showingSearchModal.value = false
  const searchModal = document.getElementById('search-modal')
  if (searchModal) {
    const modal = new Modal(searchModal)
    modal.hide()
    cleanupModalEffects()
  }
}

const performSearch = () => {
  if (searchQuery.value.trim()) {
    hasSearched.value = true
    // 鍦ㄨ繖閲屾坊鍔犲疄闄呯殑鎼滅储閫昏緫
    // 鍙互璺敱鍒版悳绱㈢粨鏋滈〉锛屼緥濡傦細
    router.push({
      path: '/search',
      query: { q: searchQuery.value }
    })
    
    // 鍏抽棴鎼滅储妯℃€佹
    showingSearchModal.value = false
    const searchModal = document.getElementById('search-modal')
    if (searchModal) {
      const modal = new Modal(searchModal)
      modal.hide()
      cleanupModalEffects()
    }
  }
}

// 鎼滅储鐩稿叧鍙橀噺鍜屾柟娉?
const searchQuery = ref('')
const hasSearched = ref(false)
const showingSearchModal = ref(false)
</script>

<style scoped>
.container {
  max-width: 1260px;
}

.header-container {
  border-bottom: solid 1px var(--el-menu-border-color);
}

.el-menu--horizontal {
  border-bottom: 0;
}

.el-menu {
  height: 55px;
}

.el-header {
  border-bottom: 1px solid #dcdfe6;
  height: 55px;
}

.navbar-wrapper {
  height: 55px;
}

.logo-img {
  height: 40px;
}

.logo-container>a {
  height: 28px;
  width: 128px;
}

.title {
  font-size: 1.6rem;
  font-weight: 800;
}

.title-li:hover {
  border-bottom: 1px solid #fff !important;
}

.bg-gray-hover:hover {
  border-bottom: 1px solid #fff !important;
  background-color: #f4f4f5 !important;
}
</style>
